#include "Siri.h"
#include "ui_Siri.h"
#include "c/uart.h"
#include <QTime>
#include <QDebug>
class FromMyDialog;
Siri::Siri(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Siri)
{
    ui->setupUi(this);
    this->m_pTimer=new QTimer;
    connect(&this->siriLivingTimer,&QTimer::timeout,this,[=](){
        this->hide();
        qDebug()<<"siri over"<<endl;
        this->siriLivingTimer.stop();
    });
    int ret = -1;
    ret = open_uart();
    if(ret) {
        printf("open_uart fail!\n");
        pthread_exit(0);
    }
    ret = set_opt(uart_fd, 9600, 8, 'N', 1);
    if(ret) {
        printf("set_opt err!\n");
        pthread_exit(0);
    }
    async_io_init();
}

void Siri::showSiri()
{
    siriLivingTimer.start(siriLivingTime);
    QTimer* m_pTimer = new QTimer(this);
       connect(m_pTimer,&QTimer::timeout,[=]{

           qsrand(QTime::currentTime().msec());
           QVector<int> vecData;
           vecData.append(2+qrand() % 8);
           for(int i = 0; i < 3; i++){

               vecData.append(qrand() % 80); //0~100之间都行
           }
           vecData.append(2+qrand() % 8);
           ui->widget->setData(vecData); //模拟声波数据 值在0~100的范围
       });
       m_pTimer->start(m_pTimeInterval);
       this->show();
}

void Siri::hideSiri()
{
    siriLivingTimer.stop();
    this->hide();
}

void Siri::handWork()
{

}

Siri::~Siri()
{
    delete ui;
}





/* 异步I/O初始化函数 */
void Siri::async_io_init(void)
{
    struct sigaction sigatn;
    int flag;

    /* 使能异步 I/O */
    flag = fcntl(uart_fd, F_GETFL); //使能串口的异步 I/O 功能
    flag |= O_ASYNC;
    fcntl(uart_fd, F_SETFL, flag);

    /* 设置异步 I/O 的所有者 */
    fcntl(uart_fd, F_SETOWN, getpid());
    /* 指定实时信号 SIGRTMIN 作为异步 I/O 通知信号 */
    fcntl(uart_fd, F_SETSIG, SIGRTMIN);

    /* 为实时信号 SIGRTMIN 注册信号处理函数 */
    sigatn.sa_sigaction =io_handler;       //当串口有数据可读时，会跳转到 io_handler 函数
    sigatn.sa_flags = SA_SIGINFO;
    sigemptyset(&sigatn.sa_mask);
    sigaction(SIGRTMIN, &sigatn, NULL);
}





